package yxk.task.utils;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.MetadataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.epm.eb.formplugin.report.excel.helper.ExcelXlsxReader;

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;


public class SaveSignCardsService {
    private static Log logger = LogFactory.getLog(SaveSignCardsService.class);
    /**
     * 生成原始卡
     * @param collection 原始卡数据
     */
    public void batchSaveSignCards(DynamicObjectCollection collection) {
        if (null != collection && collection.size() > 0) {
            DynamicObject[] dobjs = new DynamicObject[collection.size()];
            for (int i = 0; i < collection.size(); i++) {
                dobjs[i]=collection.get(i);
            }
            Object[] save = SaveServiceHelper.save(dobjs);
            logger.info("save{}",save);
        }

    }


    public void signApplyScheduled(Date startDate, Date endDate){
        logger.info("SaveSignCardsService.signApplyScheduled.startDate:[{}],endDate:[{}]",startDate,endDate);
        String selectProperties = "attperson,org,attcard,shiftdate,shift,attfile,attfilebo,entryentity,mustpoint," +
                "entryentity.effectivepoint,shift.cardsnum,entryentity.mustpoint,entryentity.timezone,entryentity.device";

        QFilter qFilter=new QFilter("shiftdate",QCP.large_equals ,startDate)
                .and(new QFilter("shiftdate", QCP.less_equals,endDate))
                .and(new QFilter("attfile.company.number",QCP.equals,"2010"));

        DynamicObject[] wtpmMulticards = BusinessDataServiceHelper.load("wtpm_multicard", selectProperties, qFilter.toArray());
        //数据来源
        DynamicObject signSource = BusinessDataServiceHelper.loadSingle("wtbd_signsource", "id,number", new QFilter("number", QCP.equals, "1040_S").toArray());
        logger.info("SaveSignCardsService.signApplyScheduled.wtpmMulticards{}",wtpmMulticards);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        DynamicObjectCollection collection=new DynamicObjectCollection();
        for (DynamicObject wtpmMulticard : wtpmMulticards) {
            int anInt = wtpmMulticard.getInt("shift.cardsnum");
            if(anInt>=4) {
                DynamicObject personId = wtpmMulticard.getDynamicObject("attperson"); //考勤人员ID
                DynamicObject attfileboId = wtpmMulticard.getDynamicObject("attfilebo");//考勤档案ID
                DynamicObject attfileId = wtpmMulticard.getDynamicObject("attfile");//考勤档案版本ID
                DynamicObject orgId = wtpmMulticard.getDynamicObject("org");//考勤组织ID
                String attcard = wtpmMulticard.getString("attcard");
                DynamicObjectCollection entryentity = wtpmMulticard.getDynamicObjectCollection("entryentity");
                DynamicObject pointStart = entryentity.get(0);
                Date startEffectivepoint = pointStart.getDate("effectivepoint");
                DynamicObject pointEnd1 = entryentity.get(1);
                Date pointEnd1Date = pointEnd1.getDate("effectivepoint");
                DynamicObject pointEnd2 = entryentity.get(2);
                Date pointEnd2Date = pointEnd2.getDate("effectivepoint");
                DynamicObject pointEnd = entryentity.get(entryentity.size() - 1);
                Date endEffectivepoint = pointEnd.getDate("effectivepoint");
                logger.info("SaveSignCardsService.signApplyScheduled.personIdName:{},startEffectivepoint:{},pointEnd1Date:{},pointEnd2Date:{},endEffectivepoint:{}",
                        personId.getString("name"),startEffectivepoint,pointEnd1Date,pointEnd2Date,endEffectivepoint);
                if (startEffectivepoint != null && endEffectivepoint != null && pointEnd1Date == null && pointEnd2Date == null ) {
                    for (int i = 1; i < entryentity.size() - 1; i++) {
                        DynamicObject row = entryentity.get(i);
                        //打卡时间
                        Date mustpoint = row.getDate("mustpoint");
                        DynamicObject timezone = pointStart.getDynamicObject("timezone");
                        DynamicObject data = new DynamicObject(MetadataServiceHelper.getDataEntityType("wtpd_signcard"));
                        formatter.setTimeZone(TimeZone.getTimeZone("GMT")); // 设置源时区

                        //考勤人
                        data.set("attperson", personId);
                        //考勤档案
                        data.set("attfilebo", attfileboId);
                        //考勤档案版本
                        data.set("attfile", attfileId);
                        //考勤组织
                        data.set("org", orgId);
                        //考勤卡号
                        data.set("attcard", attcard);
                        //打卡时间
                        data.set("signpoint", mustpoint);
                        //打卡来源
                        data.set("source", signSource);
                        //data.set("device", device);
                        data.set("status", "1");
                        //时区
                        data.set("timezone", timezone);

                        //打卡具体时间-0时区
                        String mustpointUtc = formatter.format(mustpoint);
                        Date parse=null;
                        try {
                            formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); // 设置目标时区
                            parse = formatter.parse(mustpointUtc);
                        }catch (Exception e){
                            logger.error("时区错误+e:[]",e);
                        }
                        data.set("signpointutc",parse);
                        collection.add(data);
                    }
                }
            }

        }
        batchSaveSignCards(collection);
    }
}
